package net.sx.minemodule.mvp.activities;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;

import com.kedll.library.addimg.ViewImgFragmentActivity01;
import com.kedll.library.contants.OnItemClickListener;
import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.alibaba.fastjson.JSON;
import com.homegoods.aroutercomment.ARouterPath;
import com.kedll.library.base.MyBaseFragmentActivity;
import com.kedll.library.crop.TakePhotoHelper;
import com.kedll.library.util.CollectionUtils;

import net.sx.minemodule.R;
import net.sx.minemodule.adapter.ApplyAfterSaleAdapter;
import net.sx.minemodule.adapter.PushScoreAdapter;
import net.sx.minemodule.databinding.MineActivityMineApplyAfterSaleBinding;
import net.sx.minemodule.mvp.bean.AfterBean;
import net.sx.minemodule.mvp.fragments.RefundDialogFragment;
import net.sx.minemodule.mvp.presenters.MineApplyAfterSalePresenter;
import net.sx.minemodule.mvp.views.MineApplyAfterSaleView;
import net.sx.minemodule.widget.EntryLayoutView;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import permissions.dispatcher.NeedsPermission;
import permissions.dispatcher.RuntimePermissions;

/**
 * 申请售后
 *
 * @author 11470
 */
@RuntimePermissions
@Route(path = ARouterPath.PATH_MINE_APPLYAFTERSALE)
public class MineApplyAfterSaleActivity extends MyBaseFragmentActivity<MineActivityMineApplyAfterSaleBinding, MineApplyAfterSalePresenter> implements MineApplyAfterSaleView, OnItemClickListener {
    public static final int MAX_COUNT = 4;
    private AfterBean model;
    private EntryLayoutView entry1, entry2;
    @Autowired
    public String AfterBean;
    private ApplyAfterSaleAdapter adapter;
    private String refundType;
    private List<String> pics;

    /**
     * 添加图片的Item
     */
    Map<String, Object> addMap = new HashMap<>();
    private TakePhotoHelper photoHelper;

    @Override

    protected int getLayout() {
        return R.layout.mine_activity_mine_apply_after_sale;
    }

    @Override
    protected void initEvent() {
        mBinding.setPresenter(mPresenter);
        adapter.setOnItemClickListener(this);
        photoHelper.setOnTakePhotoCompleteListener(picStr -> {
            adapter.clear();
            adapter.addAll(photoHelper.getImagesDatas());

            if (CollectionUtils.size(photoHelper.getImagesDatas()) < MAX_COUNT) {
                if (!adapter.getValues().contains(addMap)) {
                    adapter.getValues().add(addMap);
                }
            }
        });
        //显示类型选项
        onClicked(mBinding.entryRetreatCause, o -> mPresenter.getRefundArry());

    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        entry1 = mBinding.entryGoodsStatus;
        entry2 = mBinding.entryRetreatCause;
        entry1.setLeftTextAndRightTextRightImg("货物状态", "请选择", true);
        entry2.setLeftTextAndRightTextRightImg("退款原因", "请选择", true);
        mBinding.tvRetreatMoneyAfterSale.setText(model.getBasic().getPrice());
        mBinding.tvRetreatGoldAfterSale.setText(model.getBasic().getOceannum());

        photoHelper = new TakePhotoHelper(this);
        photoHelper.setIsCrop(false);
        adapter = new ApplyAfterSaleAdapter(getApplicationContext());
        pics = new ArrayList<>();

        if (CollectionUtils.size(pics) < MAX_COUNT) {
            addMap.put("isAdd", true);
            adapter.add(addMap);
        }
        mBinding.applyAfterPics.setAdapter(adapter);
        //TODO
    }

    @Override
    protected void initToolBar(Bundle savedInstanceState) {
        super.initToolBar(savedInstanceState);
        setTitle("申请售后");
        ARouter.getInstance().inject(this);
        model = JSON.parseObject(AfterBean, AfterBean.class);

    }

    @Override
    protected MineApplyAfterSalePresenter getPresenter() {
        return new MineApplyAfterSalePresenter(this, model);
    }

    @Override
    public void showRefundUi() {
        entry1.setVisibility(View.VISIBLE);
        mBinding.tvRetreatMoneyApply.setTextColor(getResources().getColor(R.color.color_a541c9));
        mBinding.tvRetreatMoneyApply.setBackgroundResource(R.drawable.mine_bg_2_a541c9);
        mBinding.tvRetreatAllApply.setTextColor(getResources().getColor(R.color.color_333333));
        mBinding.tvRetreatAllApply.setBackgroundResource(R.drawable.mine_bg_2_eeeeee);
    }

    @Override
    public void showReturnGoodsUi() {
        entry1.setVisibility(View.GONE);
        mBinding.tvRetreatMoneyApply.setTextColor(getResources().getColor(R.color.color_333333));
        mBinding.tvRetreatMoneyApply.setBackgroundResource(R.drawable.mine_bg_2_eeeeee);
        mBinding.tvRetreatAllApply.setTextColor(getResources().getColor(R.color.color_a541c9));
        mBinding.tvRetreatAllApply.setBackgroundResource(R.drawable.mine_bg_2_a541c9);
    }

    @Override
    public void showRefundReasonDialog(String s) {
        Bundle bundle = new Bundle();
        bundle.putString("data", s);
        RefundDialogFragment fragment = new RefundDialogFragment();
        fragment.showDialog(getSupportFragmentManager(), bundle, "RefundDialog");
        fragment.setOnItemClickListener(refundArryBean -> {
            refundType = refundArryBean.getType();
            entry2.setLeftTextAndRightTextRightImg("退款原因", refundArryBean.getTitle(), true);
        });
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        MineApplyAfterSaleActivityPermissionsDispatcher.onRequestPermissionsResult(this, requestCode, grantResults);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        photoHelper.onActivityResult(requestCode, resultCode, data);
    }

    @NeedsPermission({Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE})
    void onTakePhoto() {
        photoHelper.showPopup(4);
    }

    @Override
    public void itemClick(View v, Map<String, Object> item, int position) {
        int i = v.getId();
        if (i == R.id.iv_add_photo) {
            //添加图片
            MineApplyAfterSaleActivityPermissionsDispatcher.onTakePhotoWithPermissionCheck(this);
        } else if (i == R.id.iv_del_photo) {
            //删除图片
            photoHelper.removeImage(item);
            adapter.remove(item);
            if (!adapter.getValues().contains(addMap)) {
                adapter.getValues().add(addMap);
            }

        } else if (i == R.id.iv_photo) {
            //查看大图
            Intent intent = new Intent(this, ViewImgFragmentActivity01.class);
            intent.putExtra("selectedList", (Serializable) adapter.getValues());
            intent.putExtra("current", position);
            startActivity(intent);
            overridePendingTransition(enterAnim, exitAnim);

        } else {
        }
    }
}
